﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TranslationCoordinate
{
    public partial class Form1 : Form
    {
        Point p = new Point();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String[] s = PointA.Text.Split(',');
            p.Ax = double.Parse(s[0]);
            p.Ay = double.Parse(s[1]);
            p.D = double.Parse(Offset.Text);
            //由题意得过点A的方向角直线方程为 y=kx ==>k=Ay/Ax 平移值的斜率为Ax/Ay
            //k>0时 经过一三象限
            //过点A并垂直方向角线的至西安方程斜率为-Ax/Ay
            //过点A并垂直方向角线的至西安方程斜率为-Ax/Ay==>b=Ay-k*Ax
           
            if (p.Ay/p.Ax>0)
            {              
                if (p.D >= 0)
                {
                    double k = -p.Ax / p.Ay;
                    double b1 = p.Ay - k * p.Ax;
                    //根据平移值 算出在这条直线上离点A距离为d的坐标==>(x-Ax)²+(kx+b1-Ay)²=d²
                    //(k²+1）*x²  +  (2k*(b-Ay)-2Ax)*x   +Ax²+(b-Ay)²-d²=0
                    double a = Math.Pow(k, 2.0) + 1;
                    double b = 2 * k * (b1 - p.Ay) - 2 * p.Ax;
                    double c = Math.Pow(p.Ax, 2.0) + Math.Pow(b1 - p.Ay, 2.0) - Math.Pow(p.D, 2.0);
                    //根据求跟公式

                    double x1 = (-b + Math.Sqrt(Math.Pow(b, 2.0) - 4 * a * c))/2/a;
                    double y1 = k * x1 + b1;
                    double x2 = (-b - Math.Sqrt(Math.Pow(b, 2.0) - 4 * a * c)) / 2 / a;
                    double y2 = k * x2 + b1;
                    if (x1 > p.Ax)
                    {
                        textBox2.Text = x1.ToString("#0.00") + "," + y1.ToString("#0.00");
                    }
                    else
                    {
                        textBox2.Text = x2.ToString("#0.00") + "," + y2.ToString("#0.00");
                    }
                }

            }
            else if(p.Ay/p.Ax<0)
            {
                if (p.D > 0)
                {
                    double k = -p.Ax / p.Ay;
                    double b1 = p.Ay - k * p.Ax;
                    //根据平移值 算出在这条直线上离点A距离为d的坐标
                    //(k²+1）*x²  +  (2k*(b-Ay)-2Ax)*x   +Ax²+(b-Ay)²-d²=0
                    double a = Math.Pow(k, 2.0) + 1;
                    double b = 2 * k * (b1 - p.Ay) - 2 * p.Ax;
                    double c = Math.Pow(p.Ax, 2.0) + Math.Pow(b - p.Ay, 2.0) - Math.Pow(p.D, 2.0);
                    //根据求跟公式
                    double x1 = -b + Math.Sqrt(Math.Pow(b, 2.0) - 4 * a * c);
                    double y1 = k * x1 + b1;
                    double x2 = -b - Math.Sqrt(Math.Pow(b, 2.0) - 4 * a * c);
                    double y2 = k * x2 + b1;
                    if (x1 > p.Ax)
                    {
                        textBox2.Text = x1.ToString("#0.00") + "," + y1.ToString("#0.00");
                    }
                    else
                    {
                        textBox2.Text = x2.ToString("#0.00") + "," + y2.ToString("#0.00");
                    }
                }
            }

        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void PointA_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和逗号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(','))
            {

                e.Handled = true;
            }


            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为逗号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }

        private void Offset_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号 
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
            {

                e.Handled = true;
            }


            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            /*第一位不能为逗号*/
            if (e.KeyChar == (char)(',') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(',') != 1)
            {
                e.Handled = true;
            }
        }
    }
    
}

